angular.module("myticketsWorkApp",['ui.bootstrap','httpService']).
controller("myticketsWorkCtrl",['$scope','workBusesService','routesService','workTicketService',
function($scope,busesService,routesService,ticketOrderService){
     $scope.ticketTypes=[{name:"未过期车票"},{name:"已过期车票"}];
     $scope.selectedType=$scope.ticketTypes[0];
     $scope.deleteEable=true;
     $scope.selectedChange=function(selectedType){
         if(selectedType.name=="未过期车票"){
              $scope.getValidateTickets();
              $scope.deleteEable=true;
         }
         else{
             $scope.getInvalidateTicketsInfoByStaffId();
             $scope.deleteEable=false;
         }
     };
     $scope.refreshTicket=function(){
          $scope.selectedChange($scope.selectedType);
     };
     $scope.deleteTicket=function(){
         
        if($scope.checkedIndex==-1){
            alert("请选择");
            return;
        }
        var tmp=window.confirm("确认是否取消车票");
        if (tmp!=true) {
            return;
            };
      var ticketInfo={};
      ticketInfo.phone=13600000200;
      ticketInfo.password='liushui';
      ticketInfo.seatNumber= $scope.Tickets[$scope.checkedIndex].seatNumber;
      ticketInfo.ticketId=$scope.Tickets[$scope.checkedIndex].id;
      ticketInfo.seatName=$scope.Tickets[$scope.checkedIndex].seatName;
      ticketInfo.busId=$scope.Tickets[$scope.checkedIndex].busesId;
      ticketInfo.staffId=3000;
        ticketOrderService.adminDelete(ticketInfo).then(function(){
            alert("删除成功");
            $scope.refreshTicket();
        });
     };
     $scope.getValidateTickets=function () {
      ticketOrderService.getValidateTicketsInfoByStaffId({staffId:3000}).then(function (response) {
        $scope.Tickets=response;
      },function (response) {
        $scope.Tickets=response.body;
      }).finally(function () {
      });
    };
     $scope.getValidateTickets();
     $scope.getInvalidateTicketsInfoByStaffId=function () {
      ticketOrderService.getInvalidateTicketsInfoByStaffId({staffId:3000}).then(function (response) {
        $scope.Tickets=response;
      },function (response) {
        $scope.Tickets=response.body;
      }).finally(function () {
      });
    };
     $scope.checkedIndex = -1;
    $scope.selectChange=function(index){         
       if($scope.Tickets[index].checked==true){
            $scope.Tickets[index].checked=false;
          }
       else{
            $scope.Tickets[index].checked=true;
           }
        if($scope.Tickets[index].checked){
            $scope.checkedIndex = index;
            return;
        }
        for(var i = 0; i < $scope.Tickets.length; i++){
            var item = $scope.Tickets[i];
            if(item.checked != undefined && item.checked == true){
                $scope.checkedIndex = i;
                return;
            }
        }
        $scope.checkedIndex = -1;
    };
}]);
